<?php
// ImagiText v1.0 by Shocker, www.ShockingSoft.com
class ImagiText
 {
	var $image;
	var $data;
	var $params;
	var $linkPattern;
	var $bounds;

	function generateLink()
	{
		$encoded = base64_encode(sprintf(
			$this->linkPattern,
			base64_encode($this->params['text']),
			$this->params['width'],
			$this->params['height'],
			$this->params['background-color'],
			$this->params['transparent-background'],
			$this->params['background-alpha'],
			$this->params['font-color'],
			$this->params['font-size'],
			$this->params['font-file']
		));
		return "{$this->filename}?_t2i_data=$encoded";
	}

	function decodeData($tmpData)
	{
		preg_match_all('/(.*?)=(.*?)\\&/', base64_decode($tmpData), $_matches);
		foreach($_matches[1] as $_id => $_null)
			$this->data[$_matches[1][$_id]] = $_matches[2][$_id];
	
		$this->data['text'] = base64_decode($this->data['text']);
	}
	
	function initImage()
	{
		$this->bounds = imagettfbbox($this->data['f_s'], 0, $this->data['font'], $this->data['text']);

		// calculate width & height if set to auto
		if (!$this->data['w'] || !$this->data['h'])
		{
			$this->data['w'] = $this->data['w'] ? $this->data['w'] : $this->bounds[2] - $this->bounds[0];
			$this->data['h'] = $this->data['h'] ? $this->data['h'] : $this->bounds[1] - $this->bounds[7];
		}
		$this->image = imagecreatetruecolor($this->data['w'], $this->data['h']);
	
		// transparency
		if ($this->data['alpha'])
		{
			$alpha_value = $this->data['alphaVal'];
			imagesavealpha($this->image, true);
		} 
		else $alpha_value = 0;

		// background color
		imagefill($this->image, 0, 0, $this->string2color($this->data['back'], $alpha_value));

		// text color
		$this->data['f_c'] = $this->string2color($this->data['f_c']);
	}		

	function string2RGB($str, $decimal = false)
	{
		$tmp = array("R" => $str[1].$str[2], "G" => $str[3].$str[4], "B" => $str[5].$str[6]);
		if ($decimal)
			foreach ($tmp as $id => $val) $tmp[$id] = hexdec($val);
		return $tmp;
	}
	
	function string2color($str, $alpha_value = -1)
	{
		$RGB = $this->string2RGB($str,true);
		if ($alpha_value != -1)
			return imagecolorallocatealpha($this->image, $RGB['R'], $RGB['G'], $RGB['B'], $alpha_value);
		else	
			return imagecolorallocate($this->image, $RGB['R'], $RGB['G'], $RGB['B']);
	}
	
	function drawText()
	{
		imagettftext($this->image, $this->data['f_s'], 0, abs($this->bounds[0]), abs($this->bounds[7]), $this->data['f_c'], $this->data['font'], $this->data['text']);
	}

	function outputImage()
	{
		header("Content-type: image/png");
		imagepng($this->image);
	}

	function destroyImage()
	{
		imagedestroy($this->image);
	}

	function HTML_Tag($attributes = "")
	{
		return "<img src='" . $this->generateLink() . "' $attributes>";
	}
	
	function ImagiText()
	{
		preg_match('/(\\\\|\\/)([^\\\\\/]+\.php)/', __FILE__, $this->filename);
		$this->filename = $this->filename[2];

		$this->linkPattern = "text=%s&w=%s&h=%s&back=%s&alpha=%s&alphaVal=%s&f_c=%s&f_s=%s&font=%s&";
		$this->data = array();
		$this->params = array();
	}
 }

if (isset($_GET['_t2i_data']))
 {
	ini_set("display_errors", 1);
	$img = new ImagiText;
	$img->decodeData($_GET['_t2i_data']);
	$img->initImage();
	$img->drawText();
	$img->outputImage();
	$img->destroyImage();
 }
?>